<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Cache;

class CheckTokenIsExist
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {


       $token =  $request ->header('token');

       if(empty($token)){
           return response() ->json(['code' => 0 ,'data' => '缺少token','time' => date('Y:m:d H:i:s')]) ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
       }

        $isToken =    Cache::has($token);

       if ($isToken)
           return $next($request);
       else
            return response() ->json(['code' =>0,'data' => 'token过期','time' =>date('Y:m:d H:i:s')])->setEncodingOptions(JSON_UNESCAPED_UNICODE);

    }
}
